package foundation.back_access;

import java.util.Scanner;

public class Main2 {
    static int left = 0;
    static int down;
    static int right;
    static int up = 0;
    static int[][] nums;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        down = m - 1;
        int n = sc.nextInt();
        right = n - 1;
        nums = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                nums[i][j] = sc.nextInt();
            }
        }
        int x = 0, y = 0;
        while (true) {
            while (x >= up && x <= down) {
                // 先走下
                System.out.print(nums[x][y] + " ");
                x ++;
            }
            x --;
            left ++;
            y ++;
            if (!isLegal(x, y)) {
                break;
            }
            while (y >= left && y <= right) {
                System.out.print(nums[x][y] + " ");
                y ++;
            }
            y --;
            down --;
            x --;
            if (!isLegal(x, y)) {
                break;
            }
            while (x >= up && x <= down) {
                System.out.print(nums[x][y] + " ");
                x --;
            }
            x ++;
            right --;
            y --;
            if (!isLegal(x, y)) {
                break;
            }
            while (y >= left && y <= right) {
                System.out.print(nums[x][y] + " ");
                y --;
            }
            y ++;
            up ++;
            x ++;
            if (!isLegal(x, y)) {
                break;
            }
        }
    }

    public static boolean isLegal(int row, int col) {
        if (row < up || row > down || col < left || col > right) {
            return false;
        }
        return true;
    }
}
